package com.cubomagicogoias.site;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CuboMagicoGoiasServlet extends HttpServlet {
	private static final long serialVersionUID = 624952844530332114L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String caminho = req.getPathInfo();
		if (caminho == null)
			caminho = "/";
		DAO dao = DAO.init(req);
		if ("/google.login".equals(caminho)) {
			Sessao.googleLogin(dao, req, resp);
			return;
		}
		Sessao sessao = Sessao.init(req);
		if (caminho == null || "".equals(caminho) || "/".equals(caminho)) {
			resp.sendRedirect("/site/index.page");
			return;
		}
		if (caminho.endsWith(".page")) {
			if (!caminho.toLowerCase().contains("login"))
				sessao.set("loginRedirect", "/site" + caminho);
			serveJsp("jsp", "paginas",
					caminho.substring(0, caminho.length() - 5), req, resp);
		} else if (caminho.endsWith(".news")) {
			serveJsp("jsp", "novidades",
					caminho.substring(0, caminho.length() - 5), req, resp);
		} else {
			executaComando(sessao, dao, caminho.substring(1), req, resp);
		}
	}

	private void executaComando(Sessao sessao, DAO dao, String cmd,
			HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		boolean sucesso = false;
		Controler ctrl = null;
		try {
			@SuppressWarnings("unchecked")
			Class<Controler> clazz = (Class<Controler>) Class
					.forName("com.cubomagicogoias.controllers." + cmd);
			ctrl = clazz.newInstance();
			ctrl.sessao = sessao;
			ctrl.dao = dao;
			ctrl.req = req;
			String rd = ctrl.execute();
			resp.sendRedirect(rd);
			sucesso = true;
		} catch (Exception e) {
			throw new ServletException(e);
		} finally {
			if (ctrl != null)
				if (ctrl.dao != null)
					ctrl.dao.closeDatabase(sucesso);
		}
	}

	protected void serveJsp(String raiz, String dir, String nome,
			HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		resp.setCharacterEncoding("UTF-8");

		PrintWriter out = resp.getWriter();

		out.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");

		out.append("<html>");
		out.append("<head>");
		out.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
		out.append("<title>Cubo Mágico Goiás</title>");
		req.getRequestDispatcher("/" + raiz + "/segmentos/includes.jsp")
				.include(req, resp);
		out.append("</head>");

		out.append("<body>");

		req.getRequestDispatcher("/" + raiz + "/segmentos/cabecalho.jsp")
				.include(req, resp);

		out.append("<section class=\"row\" id=\"styled\">");
		out.append("<div>");
		req.getRequestDispatcher("/" + raiz + "/" + dir + nome + ".jsp")
				.include(req, resp);
		out.append("</div>");
		out.append("<div id=\"barra_lateral\">");
		req.getRequestDispatcher("/" + raiz + "/segmentos/barra.jsp").include(
				req, resp);
		out.append("</div>");
		out.append("</section>");

		req.getRequestDispatcher("/" + raiz + "/segmentos/rodape.jsp").include(
				req, resp);

		out.append("</body></html>");

	}
}
